﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;
using MyLib;
namespace WebPortal
{
    public partial class AssignmentList : System.Web.UI.Page
    {
        private StringBuilder StrSQL;
        protected void Page_Load(object sender, EventArgs e)
        {
            //RadioButton1.Text = Workflow.Resources.lang.AssignmentList_SelectTaskAssign;
            //RadioButton3.Text = Workflow.Resources.lang.AssignmentList_FutureTasksAssign;
            //RadioButton4.Text = Workflow.Resources.lang.AssignmentList_ProcessAssign;
            Button1.Text = Workflow.Resources.lang.btn_Search;
            Button3.Text = Workflow.Resources.lang.btn_Reset;
            Button4.Text = Workflow.Resources.lang.btn_RecallTask;
            Button5.Text = Workflow.Resources.lang.Assign_BackButton;
            if (!IsPostBack)
            {
                BingProcess();
            }
        }

        private void BingProcess()
        {
            StrSQL = new StringBuilder();
            StrSQL.AppendLine("select PROCESSNAME from INITIATE");
            dropProcessName.DataSource = DataAccess.Instance("UltDB").ExecuteDataTable(StrSQL.ToString());
            dropProcessName.DataTextField = "PROCESSNAME";
            dropProcessName.DataValueField = "PROCESSNAME";
            dropProcessName.DataBind();
            dropProcessName.Items.Insert(0, new ListItem("", ""));
        }

        private void BingAaaignList()
        {
            try
            {
                this.GetSelectSQL();
                DataTable dt;
                if (RadioButton1.Checked)
                {
                    task.Visible = true;
                    FutureTasks.Visible = false;
                    Processes.Visible = false;
                    dt = DataAccess.Instance("UltDB").ExecuteDataTable(StrSQL.ToString());
                    TaskList.DataSource = dt;
                    TaskList.DataBind();
                }
                else if (RadioButton3.Checked)
                {
                    task.Visible = false;
                    FutureTasks.Visible = true;
                    Processes.Visible = false;
                    dt = DataAccess.Instance("UltDB").ExecuteDataTable(StrSQL.ToString());
                    FutureTasksList.DataSource = dt;
                    FutureTasksList.DataBind();
                }
                else if (RadioButton4.Checked)
                {
                    task.Visible = false;
                    FutureTasks.Visible = false;
                    Processes.Visible = true;
                    dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSQL.ToString());
                    ProcessesList.DataSource = dt;
                    ProcessesList.DataBind();
                }
                else
                {
                    task.Visible = false;
                    FutureTasks.Visible = false;
                    Processes.Visible = false;
                    MessageBox("alert('" + Workflow.Resources.lang.AssignmentList_NotTaskMsg + "');");
                }
            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        private void MessageBox(string script)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "javascript", "<script language='javascript' defer>" + script + "</script>");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BingAaaignList();
        }

        private void GetSelectSQL()
        {
            StrSQL = new StringBuilder();

            string UserAccount = Portal.Logic.SSOLogic.GetLoginName().Replace("\\", "/");
            string strWhere = "";
            if (dropProcessName.SelectedValue.Trim() != "")
            {
                strWhere += " and PROCESSNAME = '" + dropProcessName.SelectedValue.Trim() + "' ";
            }
            if (txtAssignUserAccount.Value.Trim() != "")
            {
                strWhere += " and ASSIGNEDTOUSER = '" + txtAssignUserAccount.Value.Trim() + "' ";
            }
            if (RadioButton1.Checked)//单个任务
            {
                StrSQL.AppendLine("SELECT TASKID,PROCESSNAME,INCIDENT,STEPLABEL,ASSIGNEDTOUSER FROM TASKS WHERE STATUS=1 and TASKUSER='" + UserAccount + "' and TASKUSER!=ASSIGNEDTOUSER" + strWhere);
            }
            else if (RadioButton3.Checked)//按照时间指派或所有任务指派
            {
                StrSQL.AppendLine("SELECT PROCESSNAME,STEPLABEL,ASSIGNEDTOUSER,ASSIGNUNTIL FROM ASSIGNMENT WHERE ASSIGNUNTIL is not null and TASKUSER='" + UserAccount + "' " + strWhere);
            }
            else if (RadioButton4.Checked)//按照流程和时间指派
            {
                StrSQL.AppendLine("SELECT ID,ProcessName, TskAssignUser,BgDate,EdDate FROM WF_ASSIGNMENT t inner join v_employee a on a.ShortName = t.tskassignuser where IsEnable=1 AND  '"+DateTime.Now.ToString()+"' between t.bgdate and t.eddate and t.tskuser ='" + UserAccount + "'" + strWhere);
            }
        }



        private void RecallTasks(string strWhere)
        {
            try
            {
                GetRecallTasksSQL(strWhere);
                int count = 0;
                if (RadioButton4.Checked)
                {
                    count = DataAccess.Instance("BizDB").ExecuteNonQuery(StrSQL.ToString());
                }
                else
                {
                    count = DataAccess.Instance("UltDB").ExecuteNonQuery(StrSQL.ToString());
                }
                if (count > 0)
                {
                    BingAaaignList();
                }
                MessageBox("alert('收回成功！');");
            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        private void GetRecallTasksSQL(string strWhere)
        {
            StrSQL = new StringBuilder();
            if (RadioButton1.Checked)//单个任务
            {
                StrSQL.AppendLine("UPDATE TASKS SET ASSIGNEDTOUSER=TASKUSER WHERE STATUS=1" + strWhere);
            }
            else if (RadioButton3.Checked)//按照时间指派或所有任务指派
            {
                StrSQL.AppendLine("DELETE ASSIGNMENT WHERE 1=1" );
                //  StrSQL.AppendLine("UPDATE TASKS SET ASSIGNEDTOUSER=TASKUSER WHERE STATUS=1" + strWhere);
            }
            else if (RadioButton4.Checked)//按照流程和时间指派
            {
                StrSQL.AppendLine("update WF_ASSIGNMENT set IsEnable=0 WHERE 1=1" + strWhere);
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                string strWhere = "";
                if (RadioButton1.Checked)//单个任务
                {
                    strWhere += " and TaskID in (";
                    foreach (RepeaterItem item in TaskList.Items)
                    {
                        HtmlInputCheckBox cb = item.FindControl("Task_checkbox") as HtmlInputCheckBox;
                        if (cb.Checked)
                        {
                            strWhere += "'" + cb.Value + "',";
                        }
                    }
                    strWhere = strWhere.Substring(0, strWhere.LastIndexOf(','));
                    strWhere += ")";
                }
                else if (RadioButton3.Checked)//按照时间指派或所有任务指派
                {
                    string ProcessNames = " and isnull(PROCESSNAME,' ') in (";
                    string StepNames = " and isnull(STEPLABEL,' ') in (";
                    string AssignedToUsers = " and ASSIGNEDTOUSER in (";
                    string DueTimes = " and ASSIGNUNTIL in ";
                    foreach (RepeaterItem item in FutureTasksList.Items)
                    {
                        ProcessNames += "' " + (item.FindControl("FutureTasksList_ProcessName") as Label).Text.Trim() + "',";
                        StepNames += "' " + (item.FindControl("FutureTasksList_StepName") as Label).Text.Trim() + "',";
                        AssignedToUsers += "'" + (item.FindControl("FutureTasksList_AssignedToUser") as Label).Text.Trim() + "',";
                        DueTimes += "('" + Convert.ToDateTime((item.FindControl("FutureTasksList_Assignuntil") as Label).Text.Trim()).ToShortDateString() + "','yyyy-MM-dd'),";
                    }
                    ProcessNames = ProcessNames.Substring(0, ProcessNames.LastIndexOf(','));
                    ProcessNames += ")";
                    StepNames = StepNames.Substring(0, StepNames.LastIndexOf(','));
                    StepNames += ")";
                    AssignedToUsers = AssignedToUsers.Substring(0, AssignedToUsers.LastIndexOf(','));
                    AssignedToUsers += ")";
                    DueTimes = DueTimes.Substring(0, DueTimes.LastIndexOf(','));
                    DueTimes += "";
                    strWhere += ProcessNames + " " + StepNames + " " + AssignedToUsers + " " + DueTimes;
                }
                else if (RadioButton4.Checked)//按照流程和时间指派
                {
                    strWhere += " and ID in (";
                    foreach (RepeaterItem item in ProcessesList.Items)
                    {
                        HtmlInputCheckBox cb = item.FindControl("Processes_checkbox") as HtmlInputCheckBox;
                        if (cb.Checked)
                        {
                            strWhere += "'" + cb.Value + "',";
                        }
                    }
                    strWhere = strWhere.Substring(0, strWhere.LastIndexOf(','));
                    strWhere += ")";
                }
                RecallTasks(strWhere);
            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }
    }
}